﻿using Microsoft.AspNetCore.Mvc;

namespace iTool_Cetector.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class iToolController : ControllerBase
    {
        const string _iToolServiceProcessPath = "/var/iTool/service.process";
        const string _iToolConnectorProcessPath = "/var/iTool/client.process";

        [HttpPost("config")]
        public void Config(iToolConfigure config) 
        {
            GlobalUtils.iToolConfigure = config;
        }

        [HttpPost("service_BeginStop")]
        public void service_BeginStop()
        {
            if (System.IO.File.Exists(_iToolServiceProcessPath))
            {
                System.IO.File.WriteAllText(_iToolServiceProcessPath, "BeginStop");
            }
            else
            {
                System.IO.File.Create(_iToolServiceProcessPath).Close();
                System.IO.File.WriteAllText(_iToolServiceProcessPath, "BeginStop");
            }
        }

        [HttpPost("service_BeginStart")]
        public void service_BeginStart()
        {
            if (System.IO.File.Exists(_iToolServiceProcessPath))
            {
                System.IO.File.WriteAllText(_iToolServiceProcessPath, "BeginStart");
            }
            else
            {
                System.IO.File.Create(_iToolServiceProcessPath).Close();
                System.IO.File.WriteAllText(_iToolServiceProcessPath, "BeginStart");
            }
        }

        [HttpPost("client_BeginStop")]
        public void client_BeginStop()
        {
            if (System.IO.File.Exists(_iToolConnectorProcessPath))
            {
                System.IO.File.WriteAllText(_iToolConnectorProcessPath, "BeginStop");
            }
            else
            {
                System.IO.File.Create(_iToolConnectorProcessPath).Close();
                System.IO.File.WriteAllText(_iToolConnectorProcessPath, "BeginStop");
            }
        }

        [HttpPost("client_BeginStart")]
        public void client_BeginStart()
        {
            if (System.IO.File.Exists(_iToolConnectorProcessPath))
            {
                System.IO.File.WriteAllText(_iToolConnectorProcessPath, "BeginStart");
            }
            else
            {
                System.IO.File.Create(_iToolConnectorProcessPath).Close();
                System.IO.File.WriteAllText(_iToolConnectorProcessPath, "BeginStart");
            }
        }

    }
}
